package tech.aistar.service.client.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import tech.aistar.entity.User;
import tech.aistar.mapper.client.UserMapper;
import tech.aistar.service.client.IUserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lya
 * @Description: 本类的作用描述:用户的service层实现类
 * @CreateDate: 2019/9/30
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    /**
     * 查询所有的用户
     */
    @Override
    public List<User> findAll() {
        List<User> list = userMapper.findAll();
        return list;
    }

    /**
     * 根据用户ID查询
     */
    @Override
    public User findById(Integer id) {
        User user = userMapper.findById(id);
        return user;
    }

    /**
     * 根据用户名查询(手机号）
     */
    @Override
    public User findByUsername(String username) {
        User user = userMapper.findByUsername(username);
        return user;
    }

    /**
     * 根据手机号和密码查询
     */
    @Override
    public User findByUsernameAndPassword(String username,String password) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        User user = userMapper.findByUsernameAndPassword(map);
        return user;
    }

    /**
     * 添加用户 - 密码加密
     */
    @Override
    public void add(User user) {
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        userMapper.add(user);
    }

    /**
     * 通过ID删除用户
     */
    @Override
    public void updateStatus(User user) {
        userMapper.updateStatus(user);
    }

    /**
     * 通过ID修改用户
     */
    @Override
    public void modifiy(User user) {
        userMapper.modifiy(user);
    }

    @Override
    public User selectById(Integer id) {
        User u = userMapper.selectById(id);
        return u;
    }

    @Override
    public void updateById(User user) {
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        userMapper.updateById(user);

    }


}
